package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.beans.Person;
import com.bussiness.AdminBussinessProxy;
import com.bussiness.MovieBussinessProxy;
import com.bussiness.PersonBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;

/**
 * Servlet implementation class SearchPersonServlet
 */
public class SearchPersonServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminBussinessProxy adminBI = new AdminBussinessProxy(); 
	MovieBussinessProxy movieBI = new MovieBussinessProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchPersonServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		RequestDispatcher dispatcher;
		Person[] person = null;
		String[] ids;
		
		try
		{
			Integer catId = Integer.parseInt(request.getParameter("category"));
			String input = request.getParameter("inputstring");
			System.out.println("Category Id:"+ catId);
			System.out.println("Input String:"+ input);
			adminBI.setEndpoint(ServiceEndPoints.AdminEndPoint);
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			if(catId == 1){
			person = adminBI.getAllMembersByFname(input);
				if(person != null){
					for(int i=0; i<person.length;i++){
						ids = person[i].getIssuedMoviesList().split(",");
						
						StringBuffer sb = new StringBuffer();
						for(int j=0;j<ids.length;j++)
						{
							Movie m = movieBI.getMovieById(Integer.parseInt(ids[j]));
							sb.append(","+m.getMovieName());
						}
						person[i].setIssuedMoviesList(sb.toString());
				    }		
			    }
				

			}
			if(catId == 2){
				person = adminBI.getAllMembersByLname(input);
				if(person != null){
					for(int i=0; i<person.length;i++){
						ids = person[i].getIssuedMoviesList().split(",");
						
						StringBuffer sb = new StringBuffer();
						for(int j=0;j<ids.length;j++)
						{
							Movie m = movieBI.getMovieById(Integer.parseInt(ids[j]));
							sb.append(","+m.getMovieName());
						}
						person[i].setIssuedMoviesList(sb.toString());
				    }		
			    }
				
			}
			if(catId == 3){
				person = adminBI.getAllMembersByCity(input);
				if(person != null){
					for(int i=0; i<person.length;i++){
						ids = person[i].getIssuedMoviesList().split(",");
						
						StringBuffer sb = new StringBuffer();
						for(int j=0;j<ids.length;j++)
						{
							Movie m = movieBI.getMovieById(Integer.parseInt(ids[j]));
							sb.append(","+m.getMovieName());
						}
						person[i].setIssuedMoviesList(sb.toString());
				    }		
			    }
				
			}
			if(catId == 4){
				person = adminBI.getAllMembersByState(input);
				if(person != null){
					for(int i=0; i<person.length;i++){
						ids = person[i].getIssuedMoviesList().split(",");
						
						StringBuffer sb = new StringBuffer();
						for(int j=0;j<ids.length;j++)
						{
							Movie m = movieBI.getMovieById(Integer.parseInt(ids[j]));
							sb.append(","+m.getMovieName());
						}
						person[i].setIssuedMoviesList(sb.toString());
				    }		
			    }
				
			}
			if(catId == 5){
				Integer zip = Integer.parseInt(input);
				person = adminBI.getAllMembersByZip(zip);
				if(person != null){
					for(int i=0; i<person.length;i++){
						ids = person[i].getIssuedMoviesList().split(",");
						
						StringBuffer sb = new StringBuffer();
						for(int j=0;j<ids.length;j++)
						{
							Movie m = movieBI.getMovieById(Integer.parseInt(ids[j]));
							sb.append(","+m.getMovieName());
						}
						person[i].setIssuedMoviesList(sb.toString());
				    }		
			    }
				
			}
			
			
			session.setAttribute("selectedPersons", person);
				
			dispatcher = getServletContext().getRequestDispatcher("/searchcustomer.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/searchcustomer.jsp");
			dispatcher.forward(request, response);
		}
	

	
	}

}
